package com.likianta.anykey;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.thinkcool.circletextimageview.CircleTextImageView;

/**
 * Created by Likianta_Dodoora on 2017/12/24 0024.
 */

public class PlayAnimations {
    
    private Context context;
    private Button button1;
    private Button button2;
    
    public PlayAnimations(Context context) {
        this.context = context;
    }
    
    public PlayAnimations(Button button1, Button button2) {
        this.button1 = button1;
        this.button2 = button2;
    }
    
    public void inPlayAvatar(CircleTextImageView avatar, ImageView avatarBg) {
        /*
         * 如果实际测试发现动画在第一次执行时没反应，或者动画始终不起作用（特别是一些比较大的view在执行动画时）
         * 那么就先执行一下 clearAnimation，一般就能解决此问题
         * 具体是什么触发原理还不太清楚，总之有些view不clear也能用（比如chevron），有的是不clear的话第一次会没反应（比如avatarBg），有的是始终没反应（比如mask）
         */
        avatarBg.clearAnimation();
        Animation avatar_bg_in = AnimationUtils.loadAnimation(avatarBg.getContext(), R.anim.avatar_bg_in);
        avatarBg.setAnimation(avatar_bg_in);
        avatar_bg_in.startNow();
        
        /*
         * ◆ 关于属性动画的反向执行：
         * 根据实际测试，属性动画的 setRepeatMode 设置为 REVERSE 会形成钟摆效果，但并不能做到真正的反向执行
         * 因此要想做 in 动画和 out 动画，需要手动去写两套动画（把 from 和 to 的值颠倒过来就行了）
         * ◆ 关于属性动画 translation 值的参照系：
         * 属性位移动画的数值是相对于原始位置的偏移值。比如头像原始位置O是居中的，那么 from 0 to -100 就是从原居中位置到左移100px的位置
         * 我们假设上述移到的位置是“位置A”，现在我们想回到初始位置O，那么应该用 from -100 to 0 来回归，而不可以用 from 0 to 100
         * 简而言之，位移动画是以最最初的位置作为参考系来计算相对坐标的
         */
        
        final float PX = avatar.getWidth() / 240;
        final float _X = -324 * PX;
        final float _Y = -24 * PX;
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(avatar, "scaleX", 0.9f, 1.0f);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(avatar, "scaleY", 0.9f, 1.0f);
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(avatar, "translationX", _X, 0);
        ObjectAnimator anim4 = ObjectAnimator.ofFloat(avatar, "translationY", _Y, 0);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(anim1, anim2, anim3, anim4);
        set.setDuration(200);
        set.start();
    }
    
    public void inPlayChevron(ImageView chevron) {
        chevron.setElevation(0.1f);
        chevron.setImageResource(R.drawable.ic_chevron_fill);
        chevron.setBackgroundResource(R.drawable.chevron_bg_activated);
        Animation chevron_in = AnimationUtils.loadAnimation(chevron.getContext(), R.anim.chevron_in);
        chevron.setAnimation(chevron_in);
        chevron_in.startNow();
    }
    
    public void inPlayMask(ImageView mask) {
        mask.setVisibility(View.VISIBLE);
        Animation mask_in = AnimationUtils.loadAnimation(mask.getContext(), R.anim.mask_in);
        mask.setAnimation(mask_in);
        mask_in.startNow();
    }
    
    public void outPlayAvatar(CircleTextImageView avatar, ImageView avatarBg) {
        avatarBg.clearAnimation();
        Animation avatar_bg_out = AnimationUtils.loadAnimation(avatarBg.getContext(), R.anim.avatar_bg_out);
        avatarBg.setAnimation(avatar_bg_out);
        avatar_bg_out.startNow();
        
        final float PX = avatar.getWidth() / 240;
        final float _X = -324 * PX;
        final float _Y = -24 * PX;
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(avatar, "scaleX", 1.0f, 0.9f);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(avatar, "scaleY", 1.0f, 0.9f);
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(avatar, "translationX", 0, _X);
        ObjectAnimator anim4 = ObjectAnimator.ofFloat(avatar, "translationY", 0, _Y);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(anim1, anim2, anim3, anim4);
        set.setDuration(200);
        set.start();
    }
    
    public void outPlayChevron(ImageView chevron) {
        chevron.clearAnimation();
        chevron.setElevation(0);
        chevron.setImageResource(R.drawable.ic_chevron);
        chevron.setBackgroundResource(R.drawable.chevron_bg_normal);
    }
    
    public void outPlayMask(ImageView mask) {
        mask.clearAnimation(); // 一定要先 clear，然后再才能设置其 visibility = GONE 属性；否则其属性将无效
        mask.setVisibility(View.GONE);
        
        //Animation mask_out = AnimationUtils.loadAnimation(mask.getContext(), R.anim.mask_out);
        //mask.setAnimation(mask_out);
        //mask_out.startNow();
        //mask.clearAnimation();
    }
    
}